#pragma once
#include "../im.h"
#include "IMSession.h"
class IMSessionCache
{
private:
    static IMSessionCache *instance_;
    std::map<std::string, IMSessionPtr> sessions_;

public:
    static IMSessionCache *getInstance()
    {
        if (instance_ == nullptr)
        {
            instance_ = new IMSessionCache();
        }
        return instance_;
    }

    void add(const std::string &token, IMSessionPtr session)
    {
        sessions_[token] = session;
    }

    void remove(const std::string &token)
    {
        auto it = sessions_.find(token);
        if (it != sessions_.end())
        {
            sessions_.erase(token);
        }
    }

   IMSessionPtr get(const std::string &token)
    {
        // std::lock_guard<std::mutex> lock(mutex_);
        auto it = sessions_.find(token);
        if (it != sessions_.end())
        {
            return it->second;
        }
        return nullptr;
    }

    size_t size() const
    {
        return sessions_.size();
    }
};